<?php
/**

 */
class Admin_UserController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$request=$this->getRequest();  

		$form = new Form_UserForm();
		$this->view->form = $form;
    	
		$user_id = $this->getUserId();
		$user_role = 'admin';

		$model_client = Model_UserFactory::create($user_role);

		
		if($request->isPost()){
			$data = $request->getParams();
			if ($data['submit']=='Add User'){
				$email=$data['email'];
				$password=$data['password'];
				$role=$data['role'];
				$timezone=$data['timezone'];
				
				$model_user = new Model_User();
				$id = $model_user->addUser($email, $timezone, $role, $password);
    			if ($id)
				{
				   $this->view->success_msg="New user has been added Successfully!";
				}else{
				   $this->view->error_msg="Sorry, add user failed.";					
				}				
				
			}else if ($data['submit']=='Update User'){
				$user_id=$data['user_id'];
				$email=$data['email'];
				$password=$data['password'];
				$role=$data['role'];
				$timezone=$data['timezone'];
				
				if (($password=='')or($password=='**********'))
					$password = null;
					
				$model_user = new Model_User();
				$update = $model_user->updateUser($user_id, $email, $timezone, $role, $password);
    			if ($update)
				{
				   $this->view->success_msg="User data has been added Successfully!";
				}else{
				   $this->view->error_msg="Sorry, update user failed.";					
				}				
			}
			
		}
				
		$all_users = $model_client->getAllUser();
		
		$this->view->all_users=$all_users;
		    	
    }
    
    
	public function logoutAction()
	{
		$session = $this->getSession();
		$session->unsetAll();

		$this->_redirect('/');
	}

    
}